//用户状态
{
  window.onload = function () {
    let oLogin = document.querySelector('.login1')
    let flag = localStorage.getItem('token')
    let user = localStorage.getItem('user')
    if (flag) {
      oLogin.innerHTML = `<a class="red" href="javascript:;">Hi, ${user}</a><a class="exit" href="javascript:;">&nbsp;&nbsp; [退出]&nbsp;&nbsp;</a><em class="card"></em><a href="./html/shopping.html">购物车</a>&nbsp;<b class='red'></b>`

      let oExit = document.querySelector('.exit')

      oExit.addEventListener('click', function () {
        localStorage.removeItem('user')
        localStorage.removeItem('token')
        location.reload()
      })
      // 购物车数据
      let shopCarCount = document.querySelector('.welcome b')
      // 购物车里面
      if (localStorage.getItem('shopArr') === null) {
        var count = 0
      } else {
        count = JSON.parse(localStorage.getItem('shopArr')).length
      }
      shopCarCount.innerHTML = count
    }
  }
}
//导航二级
{
  let province = document.querySelector('.province')
  let goWhere = document.querySelector('.go-where')
  //初始化
  ajax({
    url: '../../Dangdangshopping/data/province.json',
    success(res) {
      let obj = JSON.parse(res).data
      goWhere.innerHTML = `送至： ${obj[0]}`
      //渲染省份
      obj.forEach(ele => {
        province.innerHTML += `<li><a href="javascript:;">${ele}</a></li>`
      })
      //更换省份
      province.addEventListener('click', e => {
        goWhere.innerHTML = `送至： ${e.target.innerText}`
      })
      goWhere.addEventListener('mouseover', () => {
        province.style.display = 'block'
      })
      goWhere.addEventListener('mouseout', () => {
        province.style.display = 'none'
      })
      province.addEventListener('mouseover', () => {
        province.style.display = 'block'
      })
      province.addEventListener('mouseout', () => {
        province.style.display = 'none'
      })
    },
  })
  //导航栏初始化
  let navRight = document.querySelector('.nav-right')
  ajax({
    url: '../../Dangdangshopping/data/nav.json',
    success(res) {
      let obj = JSON.parse(res).data
      obj.forEach((ele, index) => {
        navRight.innerHTML += `<li>
    <a href="javascript:;" class="${ele.erJi ? 'bg' : ''}"
    data-s=${ele.erJi ? index : null}>
    ${ele.msg}</a>
    <div class="nav-list-right"></div></li>`
      })
      let navErj = navRight.querySelectorAll('.nav-list-right')
      //生成二级
      navRight.addEventListener('mouseover', e => {
        if (e.target.tagName === 'A') {
          if (e.target.dataset.s !== 'null') {
            let erJ = e.target.parentNode.children[1]
            let erJN = obj[e.target.dataset.s].erJi.msg
            erJ.innerHTML = ''
            for (let i = 0; i < erJN.length; i++) {
              erJ.innerHTML += `<p>${erJN[i]}</p>`
            }
            erJ.style.display = 'block'
          }
        }
      })
      navRight.addEventListener('mouseout', e => {
        if (e.target.tagName === 'A') {
          e.target.nextElementSibling.style.display = 'none'
        }
      })
      navErj.forEach(ele => {
        ele.addEventListener('mouseover', e => {
          ele.style.display = 'block'
        })
        ele.addEventListener('mouseout', e => {
          ele.style.display = 'none'
        })
      })
    },
  })
}
//搜索框
{
  let searchSpan = document.querySelector('.search span')
  let selectPop = document.querySelector('.search .select_pop')
  //初始化
  ajax({
    url: '../../Dangdangshopping/data/search.json',
    success(res) {
      let obj = JSON.parse(res).data
      searchSpan.innerHTML = obj[0]
      obj.forEach(ele => {
        selectPop.innerHTML += `<a href="javascript:;">${ele}</a>`
      })
      searchSpan.addEventListener('mouseover', () => {
        selectPop.style.display = 'block'
      })
      searchSpan.addEventListener('mouseout', () => {
        selectPop.style.display = 'none'
      })
      selectPop.addEventListener('mouseover', () => {
        selectPop.style.display = 'block'
      })
      selectPop.addEventListener('click', e => {
        searchSpan.innerHTML = e.target.innerText
      })
      selectPop.addEventListener('mouseout', () => {
        selectPop.style.display = 'none'
      })
    },
  })

  //热搜数据
  let hotSearch = [
    '开口说话绘本',
    '百丽童鞋',
    '三只松鼠',
    '世界喧嚣',
    '透视世界',
    '今日抢购',
  ]
  //初始化
  let hotSech = document.querySelector('.search_bottom')
  hotSech.innerHTML = `<a href="javascript:;">热搜:</a>`
  hotSearch.forEach(ele => {
    hotSech.innerHTML += `<a href="javascript:;">${ele}</a>`
  })
  hotSech.innerHTML += `<a href="javascript:;">高级搜索</a>`
}
//购物车
{
  // 购物车数据
  let shopCarCount = document.querySelector('.char b')
  // 购物车里面
  if (localStorage.getItem('shopArr') === null) {
    var count = 0
  } else {
    count = JSON.parse(localStorage.getItem('shopArr')).length
  }
  shopCarCount.innerHTML = count
}
